package com.woniu.inlet.subscribe;

import com.rabbitmq.client.Channel;
import com.woniu.outlet.dao.PotentialCustomerReliableMapper;
import com.woniu.outlet.dao.WorkerMapper;
import com.woniu.outlet.po.PotentialCustomerReliable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class PotentialSubscribe {

    @Autowired
    private PotentialCustomerReliableMapper potentialCustomerReliableMapper;

    @Autowired
    private WorkerMapper workerMapper;

    @RabbitListener(queues = {"potentialcustomer_queue"})
    public void receivePotentialMessage(Message message, Channel channel) throws IOException {
        String sign=null;
        //首先赶紧插入数据库 如果失败，证明有了 这个时候什么都不做，直接告诉队列把该消息删除
        try{
            PotentialCustomerReliable potentialCustomerReliable = new PotentialCustomerReliable();
            System.out.println(message);
            String str=new String(message.getBody());
            System.out.println(str);
            String[] split = str.split(",");
            String reliableid = split[0];
            sign = split[1];
            potentialCustomerReliableMapper.addreliable(Integer.parseInt(reliableid));
        } catch (Exception e) {
            //如果插入失败，证明队列已经有该条消息了，此时队列直接删除消息，拒签 同时不重发
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }

        if(sign.equals("handleapplications")){
            log.info("营销经理同意了你的申请！！！");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false );
        }else if(sign.equals("distributionpotentialcustomer")){
            log.info("营销经理直接对潜在客户进行分配！！！");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false );
        }else if(sign.equals("addregularcustomer")){
            log.info("添加正式客户完成！！！");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false );
        }else if(sign.equals("agreevipapplication")){
            log.info("客户成为vip客户！！！");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false );
        }else if(sign.equals("warn")){
            log.info("你已经逾期了，请马上还钱！！！");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false );
        }else if(sign.equals("addpersoninfo")){
            log.info("亲爱的用户你已经成为正式的客户！！");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false );
        }

    }

}
